<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        if(!$this->is_admin_logged()) redirect(base_url(). $this->base_admin_url .'login_controller');
        $this->page_title = 'User';
        $this->table_name = 'user';
        $this->route_name = 'user_controller';
        $this->business_model = $this->user_model;
    }

    public function index()
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $this->load_view('list_template',$header_data,$view_data);
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
//        if($this->current_user_can('view_all_user')) $news = $this->business_model->get_elements();
//        else $news = $this->business_model->get_elements_created_by($this->current_authority);
        foreach($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Edit',
                'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/'.$item->id
            );
            $list[] = array(
                'can_delete' => $item->id,
                'id' => $item->id,
                'username' => $item->username,
//                'user_group_code' => $item->user_group_code,
                'note' => $item->note,
                'modified_date' => $item->modified_date,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'ID';
        $list[] = 'User Name';
//        $list[] = 'User group code';
        $list[] = 'Name';
        $list[] = 'Modified Date';
        $list[] = 'Action';
        return $list;

    }

    public function get_post_data()
    {

        $post_data = array(

            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
//            'user_group_code' => $this->input->post('user_group_code'),
            'user_group_code' => 'SUPER_ADMIN',
            'note' => $this->input->post('note'),
            'modified_date' => date("m-j-y", time())
        );
        return $post_data;

    }

    public function get_fields($id)
    {

        if($id == -1)
            $model = new $this->business_model();
        else
            $model = $this->business_model->get_element_by_id($id);

        $fields = array();

        $fields[] = array(
            'text' => 'User Name',
            'name' => 'username',
            'value' => $model->username,
            'type' => 'text'
        );

        $fields[] = array(
            'text' => 'Password',
            'name' => 'password',
            'value' =>$model->password,
            'type' => 'text'
        );

//        $fields[] = array(
//            'text' => 'User Group Code',
//            'name' => 'user_group_code',
//            'value' => $model->user_group_code,
//            'type' => 'select',
//            'options'   =>  array(
//                array_to_object(array(
//                    'text'  =>  'Super Admin',
//                    'value' =>  'SUPER_ADMIN'
//                )),
//                array_to_object(array(
//                    'text'  =>  'Diamond Staff',
//                    'value' =>  'DIAMOND_STAFF'
//                )),
//                array_to_object(array(
//                    'text'  =>  'Content Creator',
//                    'value' =>  'CONTENT_CREATOR'
//                ))
//            ),
//            'select_display'    =>  'text',
//            'select_value'      =>  'value'
//        );

        $fields[] = array(
            'text' => 'Name',
            'name' => 'note',
            'value' => $model->note,
            'type' => 'text'
        );

        $fields[] = array(
            'text' => 'Modified Date',
            'name' => 'modified_date',
            'value' => date('m-j-y', time()),
            'type' => 'date'
        );

        return $fields;
    }

}
